package top.yxqz.ssm.controller;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import top.yxqz.ssm.pojo.SSMEmp;
import top.yxqz.ssm.service.EmpService;

import java.util.List;

/**
 * @author: 余小小
 * @date: 2023/5/26
 * @description：员工的表述层
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
    @Autowired
    private EmpService service;
    /**
     * 跳转到员工列表页面
     * 获取所有员工----从数据库---controller--service（实现类）---mapper
     * 方法名字规范：方法名一般都是用同一个
     *
     * @return
     */
    @GetMapping("/")
    public String getAllEmp(Model model) {
        List<SSMEmp> emps = service.getAllEmp();
        model.addAttribute("emps", emps);
        return "emp_list";
    }

    /**
     * 分页查询
     * @param pageNum
     * @param model
     * @return
     */
    @GetMapping("/page/{pageNum}")
    public String getAllEmpPage(@PathVariable("pageNum") Integer pageNum, Model model) {
        PageInfo<SSMEmp> empPageInfo = service.getAllEmpPage(pageNum);
        model.addAttribute("empPageInfo", empPageInfo);
        return "emp_list_page";
    }

    /**
     * 删除员工
     * 1.获取删除条件----主键----id
     * /xxx/xx
     * uri:/emp/id值
     */
    @DeleteMapping("/{id}")
    public String  delEmp(@PathVariable("id") Integer empId){
        service.delEmp(empId);
        return "redirect:/emp/";
    }


}
